package com.bc004346.travelus;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.text.Editable;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.google.analytics.tracking.android.EasyTracker;

import static com.bc004346.travelus.Utility.DATE;
import static com.bc004346.travelus.Utility.log;

/**
 * Created by Renats on 3/3/14.
 * Settings
 */
public class SettingsActivity extends Activity {

    //<editor-fold desc="Members">

    private TextView mMessageText;
    private EditText mDaysToSearch;
    private EditText mAvailabilityRefreshInterval;
    private EditText mPnrRefreshInterval;
    private CheckBox mPnrRefreshEnabled;
    private CheckBox mAvailabilityRefreshEnabled;
    private CheckBox mAutoCheckinEnabled;
    private CheckBox mSendErrorReports;
    private NavigationDrawerFragment mNavigationDrawer;

    //</editor-fold>

    //<editor-fold desc="Properties">

    private String getPnrRefreshInterval() {
        Editable x = mPnrRefreshInterval.getText();
        return x == null ? "" : x.toString();
    }

    private String getAvailabilityRefreshInterval() {
        Editable x = mAvailabilityRefreshInterval.getText();
        return x == null ? "" : x.toString();
    }

    private String getDaysToSearch() {
        Editable x = mDaysToSearch.getText();
        return x == null ? "" : x.toString();
    }

    //</editor-fold>

    //<editor-fold desc="Overrides">

    @Override
    protected void onStart() {
        super.onStart();
        EasyTracker.getInstance(this).activityStart(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        EasyTracker.getInstance(this).activityStop(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        try {

            mMessageText = (TextView) findViewById(R.id.drawerMessageText);
            mDaysToSearch = (EditText) findViewById(R.id.drawerDaysToSearch);
            mAvailabilityRefreshInterval = (EditText) findViewById(R.id.drawerAvailabilityRefreshInterval);
            mPnrRefreshInterval = (EditText) findViewById(R.id.drawerPnrRefreshInterval);
            mPnrRefreshEnabled = (CheckBox) findViewById(R.id.drawerPnrRefreshEnabled);
            mAvailabilityRefreshEnabled = (CheckBox) findViewById(R.id.drawerAvailabilityRefreshEnabled);
            mAutoCheckinEnabled = (CheckBox) findViewById(R.id.drawerAutoCheckinEnabled);
            mSendErrorReports = (CheckBox) findViewById(R.id.drawerSendErrorReportsEnabled);

            TextView mName = (TextView) findViewById(R.id.drawerName);
            TextView mSeniority = (TextView) findViewById(R.id.drawerSeniority);
            Button mSaveButton = (Button) findViewById(R.id.drawerSaveButton);
            mSaveButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    HandleButtonClick();
                }
            });

            mName.setText(Utility.name);
            mSeniority.setText(DATE.format(Utility.seniority));
            mDaysToSearch.setText(Utility.daysToSearch);
            mAvailabilityRefreshInterval.setText(Utility.availabilityRefreshInterval);
            mPnrRefreshInterval.setText(Utility.pnrRefreshInterval);
            mPnrRefreshEnabled.setChecked(Utility.pnrRefreshEnabled);
            mAvailabilityRefreshEnabled.setChecked(Utility.availabilityRefreshEnabled);
            mAutoCheckinEnabled.setChecked(Utility.autoCheckinEnabled);
            mSendErrorReports.setChecked(Utility.sendErrorReportsEnabled);
            mMessageText.setText("");

            mNavigationDrawer = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.fullscreenDrawer);
            if (mNavigationDrawer != null)
                mNavigationDrawer.init();

        } catch (Exception e) {
            log(e, this);
        }
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mNavigationDrawer.syncState();
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        mNavigationDrawer.handleOnPrepareOptionsMenu(menu);
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        mNavigationDrawer.syncState();
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        mNavigationDrawer.handleOnOptionsItemSelected(item);
        return super.onOptionsItemSelected(item);
    }

    //</editor-fold>

    private void HandleButtonClick() {

        try {
            mMessageText.setText("");
            StringBuilder message = new StringBuilder();

            if (getPnrRefreshInterval() == null ||
                    Integer.valueOf(getPnrRefreshInterval()) < 1 ||
                    Integer.valueOf(getPnrRefreshInterval()) > 24)
                message.append(getString(R.string.drawerMessage_PNRRefreshInvalid));

            if (getAvailabilityRefreshInterval() == null ||
                    Integer.valueOf(getAvailabilityRefreshInterval()) < 1 ||
                    Integer.valueOf(getAvailabilityRefreshInterval()) > 60)
                message.append(getString(R.string.drawerMessage_AvailabilityRefreshInvalid));

            if (getDaysToSearch() == null ||
                    Integer.valueOf(getDaysToSearch()) < 1 ||
                    Integer.valueOf(getDaysToSearch()) > 365)
                message.append(getString(R.string.drawerMessage_DaysToSearchInvalid));

            if (message.length() > 0)
                mMessageText.setText(message.toString());
            else {
                Utility.pnrRefreshEnabled = mPnrRefreshEnabled.isChecked();
                Utility.pnrRefreshInterval = getPnrRefreshInterval();
                Utility.availabilityRefreshEnabled = mAvailabilityRefreshEnabled.isChecked();
                Utility.availabilityRefreshInterval = getAvailabilityRefreshInterval();
                Utility.autoCheckinEnabled = mAutoCheckinEnabled.isChecked();
                Utility.daysToSearch = getDaysToSearch();
                Utility.sendErrorReportsEnabled = mSendErrorReports.isChecked();
                finish();
            }
        } catch (NumberFormatException e) {
            log(e, this);
        }
    }
}
